from flask import Flask, redirect, url_for, request, render_template
app = Flask(__name__)

@app.route('/hello/<name>/')
def hello_name(name):
   return 'Hello %s!' % name

@app.route('/blog/<int:postID>/')
def show_blog(postID):
   return 'Blog Number %d' % postID

@app.route('/rev/<float:revNo>/')
def revision(revNo):
   return 'Revision Number %f' % revNo

@app.route('/admin/')
def hello_admin():
   return 'Hello Admin'

@app.route('/guest/<guest>/')
def hello_guest(guest):
   return 'Hello %s as Guest' % guest

@app.route('/user/<name>/')
def hello_user(name):
   if name2 =='admin':
      return redirect(url_for('hello_admin'))
   else:
      return redirect(url_for('hello_guest',guest = name))

@app.route('/success/<name2>')
def success(name2):
   return 'welcome %s' % name2

@app.route('/login',methods = ['POST', 'GET'])
def login():
   if request.method == 'POST':
      user = request.form['name1']
      return redirect(url_for('success',name2 = user))
   else:
      user = request.args.get('name1')
      return redirect(url_for('success',name2 = user))

@app.route('/hello')
def hello():
   # 往模板中传入的数据
   my_str = 'Hello Word'
   my_int = 10
   my_array = [3, 4, 2, 1, 7, 9]
   my_list = [1, 5, 4, 3, 2]
   my_dict = {
        'name': 'xiaoming',
        'age': 18
   }
   return render_template('hello.html',
                        my_str=my_str,
                        my_int=my_int,
                        my_array=my_array,
                        my_list=my_list,
                        my_dict=my_dict)

@app.route("/")
def index():
   return render_template("index.html")

@app.route('/student')
def student():
   return render_template('student.html')

@app.route('/fruit')
def fruit():
   return render_template('fruit_simple.html')

@app.route('/result',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      result = request.form
      return render_template("result.html",result = result)

@app.route('/performance')
def performance():
   return render_template('performance.html')

if __name__ == '__main__':
   app.run(debug=True)